package cn.edu.jxau.core.service;

import cn.edu.jxau.core.exception.ServiceException;
import cn.edu.jxau.framework.dao.core.Session;
import cn.edu.jxau.framework.dao.core.sqlSessionFactory;
import cn.edu.jxau.framework.dao.exception.ConfigurationException;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:光谱文件夹业务
 * @author：SongQingZheng
 * @create 2017 08 26 10:07
 */
public class SpectrumDirService extends ServiceSupport {

    private static final long serialVersionUID = 1L;
    private final String DIRMSG = "一个查询到0条记录";
    //日志信息
    private Logger logger = LoggerFactory.getLogger(SpectrumManageService.class);

    public SpectrumDirService() {
    }

    /**
     * @param view   service.xml中的name和视图对应
     * @param params request入口的参数数组
     * @param data   返回给Servlet的出口参数
     * @throws ServiceException
     */
    public SpectrumDirService(String view, Map<String, Object[]> params, Map<String, Object> data) throws ServiceException {

        super(view, params, data);
        execute(view, params, data); // 执行业务
    }

    /**
     * 遍历该用户的所有文件夹
     *
     * @param params
     * @param data
     */
    @SuppressWarnings({"unused", "unchecked", "rawtypes"})
    protected void listAllSpectrumDir(Map<String, Object[]> params, Map<String, Object> data) {
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        Map<String, Object> map = new HashMap<String, Object>();//存放光谱类型
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据

        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        String userId = getParamStringValue(params, "userID", 0);
        Matcher isNum = pattern.matcher(userId);
        //判断field是否为空或是否是数字
        if (null != userId && isNum.matches()) {
            int UserID = Integer.parseInt(userId);
            param.put("UserID", UserID);
        } else {
            return;
        }

        Session session = null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("queryDir", param, datas);
            data.put("spectrumType", querySpectrumType());
            data.put("detectedObject",queryDetect());
            if (datas == null && datas.isEmpty()) {
                data.put("dirStatus", "0");
                data.put("dirEmpty", DIRMSG);
                data.put("result", SUCCESS);
            } else {
                data.put("dirStatus", "1");
                data.put("spectrumDir", datas);
                data.put("result", SUCCESS);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } finally {
            session.releaseConnection();
        }
    }

    /**
     * 添加文件夹
     *
     * @param params request入口的参数数组
     * @param data   返回给Servlet的出口参数
     */
    protected void addSpectrumDir(Map<String, Object[]> params, Map<String, Object> data) {
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        String userId = getParamStringValue(params, "userID", 0);
        Matcher isNum = pattern.matcher(userId);
        //判断field是否为空或是否是数字
        if (null != userId && isNum.matches()) {
            int UserID = Integer.parseInt(userId);
            param.put("UserID", UserID);
        } else {
            return;
        }
        //判断文件名是否合法
        String fileName = getParamStringValue(params, "fileName", 0);
        if (fileName.length() < 16 && fileName.length() > 1) {
            param.put("fileName", fileName);
        } else {
            data.put("fileNameError", "您输入的名字不合法");
            return;
        }
        //判断params是否为空或是否为null,若不为空则插入数据库
        Session session = null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("insertSpectrumDir", param, datas);
            data.put("addsuccess", "添加成功");
            data.put("result", SUCCESS);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } finally {
            session.releaseConnection();
        }
    }

    /**
     * 重命名光谱文件夹
     *
     * @param params
     * @param data
     */
    protected void renameSpectrumDir(Map<String, Object[]> params, Map<String, Object> data) {
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        String dirId = getParamStringValue(params, "dirId", 0).trim();
        String newFolderName = getParamStringValue(params, "newName", 0).trim();
        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        Matcher isNum = pattern.matcher(dirId);
        if (dirId != null && isNum.matches()) {
            int dirID = Integer.parseInt(dirId);
            param.put("dirID", dirID);
            param.put("newFolderName", newFolderName);
        } else {
            return;
        }
        Session session = null;
        try {
            session = sqlSessionFactory.getInstance().getSession();

            boolean flag = session.execute("updateDir", param, datas);

            data.put("result", SUCCESS);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            session.releaseConnection();
        }
    }

    /**
     * 删除光谱文件夹
     *
     * @param params
     * @param data
     */
    protected void deleteSpectrumDir(Map<String, Object[]> params, Map<String, Object> data) {
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        String dirID = getParamStringValue(params, "dirId", 0);
        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        Matcher isNum = pattern.matcher(dirID);
        //判断dirID是否为空或是否是数字
        if (null != dirID && isNum.matches()) {
            int UserID = Integer.parseInt(dirID);
            param.put("dirID", dirID);
        } else {
            return;
        }
        Session session = null;
        try {
            session = sqlSessionFactory.getInstance().getSession();

            boolean flag = session.execute("deleteDir", param, datas);
            if (flag) {
                data.put("deletesuccess", "删除成功！");
                data.put("result", SUCCESS);
            } else {
                data.put("deleteerror", "文件夹中有光谱不能删除");
                data.put("result", SUCCESS);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     *  遍历所有的被检测物
     * @return list
     */
    protected List<HashMap<String, Object>> queryDetect(){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        Session session = null;
        try {

            session = sqlSessionFactory.getInstance().getSession();
            session.execute("queryDetect", param, datas);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }finally {
            session.releaseConnection();
        }
        return datas;
    }
    /**
     * 查询光谱类型
     *
     * @return
     */
    protected List<HashMap<String, Object>> querySpectrumType() {
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        Session session = null;
        try {

            session = sqlSessionFactory.getInstance().getSession();
            session.execute("querySpectrumType", param, datas);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }finally {
            session.releaseConnection();
        }
        return datas;
    }

}
